package cn.larry.common.config;

import liquibase.integration.spring.SpringLiquibase;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.DefaultResourceLoader;

import javax.sql.DataSource;

/**
 * This is created,
 * but not necessarily updated by me.
 * Therefore, AUTHOR is useless.
 * 2023/7/5 10:15
 * Liquibase配置类
 *
 * @Version 1.0
 */
@Slf4j
@Configuration
public class LiquibaseConfig {

    /*** 指定liquibase的版本 **/
    @Value("${spring.liquibase.contexts}")
    private String contexts;

    /*** 是否开启liquibase **/
    @Value("${spring.liquibase.enabled}")
    private Boolean enable;

    /**
     * liquibase bean声明
     */
    @Bean
    public SpringLiquibase liquibase(DataSource dataSource) {
        SpringLiquibase liquibase = new SpringLiquibase();
        // Liquibase文件路径
        liquibase.setChangeLog("classpath:liquibase/master.xml");
        liquibase.setDataSource(dataSource);
        if (StringUtils.isNotBlank(contexts)) {
            liquibase.setContexts(contexts);
        }
        liquibase.setShouldRun(enable);
        liquibase.setResourceLoader(new DefaultResourceLoader());
        return liquibase;
    }

}
